package logic

import (
	"context"
	"fmt"
	"product/model/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *productrpc.LonginRequest) (*productrpc.LonginResponse, error) {
	// todo: add your logic here and delete this line
	var findPro mysql.Product
	mysql.DB.Debug().Find(&findPro)
	if findPro.ID == 0 {
		return nil, fmt.Errorf("用户还未注册，请先去注册")
	}
	if findPro.Password != in.Password {
		return nil, fmt.Errorf("密码不正确，请重新输入")
	}

	return &productrpc.LonginResponse{Id: int64(findPro.ID)}, nil
}
